package org.mili.ticketeer.ui.ticket;

import org.freixas.jcalendar.JCalendarCombo;
import org.mili.ticketeer.dto.*;
import org.mili.ticketeer.dto.Action;
import org.mili.ticketeer.service.ActionService;
import org.mili.ticketeer.service.ServiceException;
import org.mili.ticketeer.service.ServiceFactory;
import org.mili.ticketeer.ui.FormElement;
import org.mili.ticketeer.ui.SimpleForm;
import org.mili.ticketeer.util.Log;

import javax.swing.*;


/**
 * Created with IntelliJ IDEA.
 * User: papa
 * Date: 20.07.13
 * Time: 14:10
 * To change this template use File | Settings | File Templates.
 */
public class ActionDataForm extends SimpleForm<org.mili.ticketeer.dto.Action> {
    private ActionService _actionService = ServiceFactory.getService(ActionService.class);
    private Ticket oneFormObject;


    public ActionDataForm(String label) {
        super(FormMode.NORMAL, label, new FormElement[]{
                new FormElement(Identifieable.Fields.ID, new JTextField()),
                new FormElement(org.mili.ticketeer.dto.Action.Fields.DATE, new JCalendarCombo(
                        JCalendarCombo.DISPLAY_DATE | JCalendarCombo.DISPLAY_TIME, true)),
                new FormElement(org.mili.ticketeer.dto.Action.Fields.TITLE, new JTextField()),
                new FormElement(org.mili.ticketeer.dto.Action.Fields.COMMENT, new JTextField())
        });

        JTextField id = (JTextField) getFormElement(Identifieable.Fields.ID).getComponent();
        id.setEnabled(false);

    }

    @Override
    public void save() throws ServiceException {
        Log.log(this, "save", "formObject: %s, oneFormObject=%s", formObject, oneFormObject);
        formObject.setTicket(oneFormObject.getId());
        formObject.setTitle(getString(Action.Fields.TITLE));
        formObject.setComment(getString(Action.Fields.COMMENT));
        formObject.setDate(getDate(Action.Fields.DATE));
        _actionService.write(formObject);
    }

    @Override
    public void reset() {
        if (formObject.getId() > 0) {
            onSelectionChanged(formObject);
        } else {
            onSelectionChanged(new Contact());
        }
    }

    @Override
    public void cancel() {
        reset();
    }

    @Override
    public boolean validation() {
        Log.log(this, "validation", "true");
        return true;
    }

    @Override
    public void onSelectionChanged(Object element) {
        if (element instanceof Ticket) {
            oneFormObject = (Ticket) element;
            Log.log(this, "onSelectionChanged", "ticket: %s", oneFormObject);
        } else if (element instanceof Action) {
            formObject = (Action) element;
            Log.log(this, "onSelectionChanged", "ticket: %s, action: %s", oneFormObject, formObject);
            updateTextField(Identifieable.Fields.ID, String.valueOf(formObject.getId()));
            updateTextField(Action.Fields.TITLE, formObject.getTitle());
            updateTextField(Action.Fields.COMMENT, formObject.getComment());
            updateCalendarCombo(Action.Fields.DATE, formObject.getDate());
        }
    }

}
